//
//  YHKHomeCellViewModel.swift
//  xinlangweiBo2
//
//  Created by 杨和逵 on 16/3/1.
//  Copyright © 2016年 yanghekui. All rights reserved.
//

import UIKit

class YHKHomeCellViewModel: NSObject {

    
    var statrsModel:YHKStatrsModel?
    
    //认证标识
    var verified_image:UIImage?{
        
        switch (statrsModel?.user?.verified_type ?? -1) {
        
        case -1: return nil
        case 0 : return UIImage(named: "avatar_vip")
        case 2,4,5 : return UIImage(named: "avatar_enterprise_vip")
        case 220 :return UIImage(named: "avatar_grassroot")
           
            //swfit中的default一定要写
        default: return nil
            
        }
            
        }
    
    
    //vip的头像
    var mbrank_image: UIImage? {
        
        return UIImage(named: "common_icon_membership_level\(statrsModel?.user?.mbrank ?? 0)")
    }
    
    //裁剪时间
//    var time_String:String?{
//    
//    var time = "\( statrsModel?.created_at ?? "")"
//   
//        //字符串截取，
//        time = (time as NSString).substringWithRange(NSMakeRange(11, 8))
//            
//    return time
//    
//    }
    
    //转发微博的内容
    var retweetStatusString:String?{
        
        
        return statrsModel?.retweeted_status?.text
        
    }

    //转发微博的个数
    //当有转发数的时候显示--->数量
    //当没有转发的时候显示--->转发
    var retweetString:String?{
        
        if statrsModel?.reposts_count == 0{
            return "转发"
        }else{
            
            if let str = statrsModel?.reposts_count{
                
                return "\(str)"
            }
            return "转发"
    }
    }
    
    
    //comments_count	int	评论数
    var commentsString:String?{
        
        if statrsModel?.comments_count == 0{
            return "评论"
        }else{
            
            if let set = statrsModel?.comments_count{
                
                return "\(set)"
            }
            return "评论"
        }
        
        
    }

    //attitudes_count	int	表态数
    var attitudesString:String?{
        
        if statrsModel?.attitudes_count == 0{
            return "赞"
        }else{
            
            if let set = statrsModel?.attitudes_count{
                
                return "\(set)"
            }
            return "赞"
        }
    
    }
    
    
    
    //设置原创微博里面的来源
    func dealSourec(soureceString:String?) -> String?{
        
        //进行一个判断，soureceString不为空
        guard let sourece = soureceString else{
            
            return nil
        }
        
        //分割子符串 - >
        
        let soureceArray = sourece.componentsSeparatedByString(">")
        
        //严谨一定要考虑到其他因素
        if soureceArray.count >= 2{
            
            let subSourec = soureceArray[1]
            
            //分割字符串 - <
            let lastSoureceArray = subSourec.componentsSeparatedByString("<")
            
            if lastSoureceArray.count >= 1{
                
                return lastSoureceArray.first
                
            }else{
                
                return nil
            }
            
            }else{
                
                return nil
            }
        
    }
    
    
    //原创微博的时间
    func dealCreateDate(create_date:String?) -> String?{
        
        //判断
        guard let create_Date = create_date else{
            return nil
        }
        
        //时间转换:把 Optional("Thu Mar 03 17:00:03 +0800 2016")时间转换成标准时间
        let formatter = NSDateFormatter()//获取时间这个类
        
        //设置时区
        formatter.locale = NSLocale(localeIdentifier: "cn")
        
        formatter.dateFormat = "EEE MMM dd HH:mm:ss Z yyyy"
        
        let date = formatter.dateFromString(create_Date)
        
        //获取一个当前时间的日历
        let calendar = NSCalendar.currentCalendar()
        
        //设置要获取的比较 年/月/日/时/分/秒/ 枚举
        let unites = NSCalendarUnit(arrayLiteral: .Year,.Month,.Day,.Hour,.Minute)
        
        //两个时间进行比较
        if let Odate = date{
            
            let compts = calendar.components(unites, fromDate: Odate, toDate: NSDate(), options: [])
            
        // 4.通过 返回的值,就可以获取比较的大小
            
//            print(compts.year)
//            print(compts.month)
//            print(compts.day)
//            
//            print(compts.hour)
//            print(compts.minute)
           
            if compts.year == 0 { //今年
                
                if compts.month == 0 {//这个月
                    
                    if compts.day == 0 {//今天
                        
                        if compts.hour == 0{// 1小时内
                            
                            if compts.minute == 0 {//刚刚
                                
                                return "刚刚"
                                
                            }else{//几分钟之前
                                
                              return "\(compts.minute)分钟前"
                                
                            }
                            
                            
                        }else{ //1小时外
                            
                            return "\(compts.hour)小时前"
                        }
                        
                        
                    }else if compts.day == 1{//昨天
                        
                         // 几点几分  2016-03-04 09:50:27
                        formatter.dateFormat = "HH:mm"
                        
                        let str = formatter.stringFromDate(Odate)
                        
                        return "昨天\(str)"
                        
                    }
                    
                }else{//其他月 MM-dd HH:mm
                    
                    
                    formatter.dateFormat = "MM-dd HH:mm"
                    let str = formatter.stringFromDate(Odate)
                    
                    return "\(str)"
                }
                
            }else{//其他年 MM-dd HH:mm
               
                formatter.dateFormat = "MM-dd HH:mm"
                let str = formatter.stringFromDate(Odate)
                
                return "\(str)"
                
            }
     
            
        }
        
        //返回比较的结果
        return "时间"
        
    }
    
    
    
    
    
    
}
